{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bb4a5558-7e95-475c-8d1f-414a9090c658",
   "metadata": {},
   "source": [
    "### numpy数据创建\n",
    "##### [文档](https://numpy.org/doc/stable/user/basics.creation.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d31852a9-fb5e-47be-aa01-bce644913e7d",
   "metadata": {},
   "source": [
    "numpy的数据类型是一个ndarray,ndarray中的每一个元素都是相同的，换言之，ndarray中的每一个元素的的大小是一样的。\n",
    "但是在python中，list列表中的元素可以是不同的数据类型。\n",
    "\n",
    "numpy的创建，\n",
    "1. 通过np.array()将python的list转成numpy\n",
    "   - 一个list转成一维的numpy\n",
    "   - 一个list的list转成二维的numpy\n",
    "2. 通过np中内置函数来创建numpy，zeros,ones,full等创建，提供一个行和列个数的元组\n",
    "   - 一维的，np.arange,np.linspace等\n",
    "   - 二维的，np.eys,np.diag,np.vander等\n",
    "   - 通用的，np.zeros,np.ones，np.random等\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0435e08f-d946-4ca2-b6cb-dd4c8f6c49fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 引入numpy\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a0b807a-eddc-47ea-96af-c37a60625745",
   "metadata": {},
   "source": [
    "#### 将python的list转成numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e3e07353-a66f-4d71-8fea-1d8bfafbd055",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将一个列表转成ndarray\n",
    "np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9fb6d77c-dd82-440c-a3e5-20a4d5fcaa4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将一个元组列表转成numpy\n",
    "np.array([(1,2,3),(4,5,6)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "62dd5c49-10cd-4937-b1a0-01a8759c0eea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 2, 3, 4],\n",
       "        [1, 2, 3, 4]],\n",
       "\n",
       "       [[5, 6, 7, 8],\n",
       "        [5, 6, 7, 8]],\n",
       "\n",
       "       [[1, 2, 3, 4],\n",
       "        [1, 2, 3, 4]],\n",
       "\n",
       "       [[5, 6, 7, 8],\n",
       "        [5, 6, 7, 8]]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将一个元组列表组成的列表转成numpy\n",
    "np.array([[(1,2,3,4),(1,2,3,4)],[(5,6,7,8),(5,6,7,8)],[(1,2,3,4),(1,2,3,4)],[(5,6,7,8),(5,6,7,8)]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e4e9e5e-3906-4c49-ba21-dfa258af4df8",
   "metadata": {},
   "source": [
    "#### 通过numpy内部函数创建"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3e6fcec-e214-4dee-9c3f-9c3c130c631c",
   "metadata": {},
   "source": [
    "##### 一维创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "624c4ae6-01e6-4441-a673-edd730dcf9e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过nrange来创建一个一维的区间numpy\n",
    "# 从0-10\n",
    "np.arange(0,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "04b7c8b2-3dd9-4ebe-987b-3fe3e24b92b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从0-10，步长是2\n",
    "np.arange(0,10,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f87e80e4-0d41-4cd4-8bf3-2016991d492e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  3.33333333,  6.66666667, 10.        ])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过linspace来创建一个一个维区间numpy，与arange的区别是，linspace的第三个参数是个数，而arange的第三个参数是步长\n",
    "np.linspace(0,10,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5de32da-43a0-4963-b3d0-5f0f6a24c92c",
   "metadata": {},
   "source": [
    "##### 二维创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "1cc6489b-f2e5-44a8-b0e5-f59e2f4df0c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过eys创建一个二维的numpy，行数=列数的位置为1，其余为0，\n",
    "# 如果只有一个参数，那行数列数都是一样的，都等于第一个参数。\n",
    "# 如果两个参数，你第一个是行数，第二个是列数\n",
    "np.eye(5,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "52fa20fa-2d77-4a07-9e1a-caa39869ed3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 1., 0.],\n",
       "       [0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "91b02347-2b97-41ea-b50a-2bfdd69d5375",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 1, 0, 0],\n",
       "       [0, 0, 0, 0, 5, 0],\n",
       "       [0, 0, 0, 0, 0, 9],\n",
       "       [0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过diag创建一个已给定数组为主对角线的numpy\n",
    "np.diag([1,5,9])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a00359ad-376f-401e-9206-2b6633ce3cdb",
   "metadata": {},
   "source": [
    "diag这个函数不光是能创建一个二维ndarry，同时也可以提取一个二维数组的对角线，diag有两个参数\n",
    "1. 第一个参数\n",
    "   - 如果是一维数组，那就以该数组为主对角线创建一个二维数组\n",
    "   - 第一个参数如果是一个二维数组，那就返回该二维数组的主对角线\n",
    "2. 第二个参数\n",
    "   - 如果第二个参数k值大于0，如果第一个参数是一维数组，那返回的二维数组会向前增加行数和向下增加列数，如果第一个参数是个二维数组，那就返回主对角线上方第k个对角线\n",
    "   - 如果第二个参数k值小于0，如果第一个参数是一维数组，那返回的二维数组会向后增加行数和向上增加列数，如果第一个参数是个二维数组，那就返回主对角线下方方第k个对角线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "b5d59f86-2a87-4ff6-b2ad-15ba97c91721",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 5, 9])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一个参数是一个二维数组\n",
    "np.diag([(1,2,3),(4,5,6),(7,8,9)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7765c7b6-4dfd-42fc-bbd1-e7ae5a0cadc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 6]\n",
      "[3]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# 第一个参数是一个二维数组，第二个参数是正数\n",
    "print(np.diag([(1,2,3),(4,5,6),(7,8,9)],1))\n",
    "print(np.diag([(1,2,3),(4,5,6),(7,8,9)],2))\n",
    "print(np.diag([(1,2,3),(4,5,6),(7,8,9)],3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "20ec8f78-6c48-49aa-971a-be5bcc8f4bf6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 8]\n",
      "[7]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# 第一个参数是一个二维数组，第二个参数是负数\n",
    "print(np.diag([(1,2,3),(4,5,6),(7,8,9)],-1))\n",
    "print(np.diag([(1,2,3),(4,5,6),(7,8,9)],-2))\n",
    "print(np.diag([(1,2,3),(4,5,6),(7,8,9)],-3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "6c5e1531-8b4a-4f27-aefb-68f1fa76b851",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0]\n",
      " [0 5 0]\n",
      " [0 0 9]]\n",
      "[[0 1 0 0]\n",
      " [0 0 5 0]\n",
      " [0 0 0 9]\n",
      " [0 0 0 0]]\n",
      "[[0 0 1 0 0]\n",
      " [0 0 0 5 0]\n",
      " [0 0 0 0 9]\n",
      " [0 0 0 0 0]\n",
      " [0 0 0 0 0]]\n"
     ]
    }
   ],
   "source": [
    "# 第一个参数是一个一维数组，第二个参数是正数\n",
    "print(np.diag([1,5,9]))\n",
    "print(np.diag([1,5,9],1))\n",
    "print(np.diag([1,5,9],2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "c1248303-90b3-4e7b-ac1e-6f66c5ad02f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0]\n",
      " [0 5 0]\n",
      " [0 0 9]]\n",
      "[[0 0 0 0]\n",
      " [1 0 0 0]\n",
      " [0 5 0 0]\n",
      " [0 0 9 0]]\n",
      "[[0 0 0 0 0]\n",
      " [0 0 0 0 0]\n",
      " [1 0 0 0 0]\n",
      " [0 5 0 0 0]\n",
      " [0 0 9 0 0]]\n"
     ]
    }
   ],
   "source": [
    "# 第一个参数是一个一维数组，第二个参数是负数\n",
    "print(np.diag([1,5,9]))\n",
    "print(np.diag([1,5,9],-1))\n",
    "print(np.diag([1,5,9],-2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "27950baf-5448-48f0-8975-b50e3c5ecd93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  1,  1,  1],\n",
       "       [ 8,  4,  2,  1],\n",
       "       [27,  9,  3,  1],\n",
       "       [64, 16,  4,  1]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过vendor创建范德蒙矩阵，从右想左排列，如果想从左向右，可以使用第三个参数increasing=True,第二个参数是列数。如果不指定就第一个参数的一维数组长度一样\n",
    "# 范德蒙矩阵就是以数组为每一个元素的列次幂组成的矩阵。列的表达式 an = [r1^n,r2^n,r3^n]\n",
    "np.vander([1,2,3,4],)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "d7785b12-f048-4e22-b2e4-1d6f9ed866a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  1,  1,  1],\n",
       "       [ 1,  2,  4,  8],\n",
       "       [ 1,  3,  9, 27],\n",
       "       [ 1,  4, 16, 64]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vander([1,2,3,4],increasing=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9eb6c42f-42c1-462f-a83f-0a1cfc749787",
   "metadata": {},
   "source": [
    "##### 通用创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5e650150-2719-4024-91e9-7a1cdaea1b92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建一个全是0的,3行五列\n",
    "np.zeros((3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12c4673e-66d7-462e-95ee-d0d318f3ddee",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8596f870-b5f3-425e-aab9-47168e5f05ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也可以指定类型创建，如果我们不指定类型，默认是浮点型的\n",
    "np.zeros((3,5),dtype=\"int64\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "79669bd8-d446-4a0d-adb5-6ce4a2b060d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个全是1的3行5列ndarray\n",
    "np.ones((3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "579e154f-e2f7-4a73-a210-0cac878a606c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 8, 8, 8, 8],\n",
       "       [8, 8, 8, 8, 8],\n",
       "       [8, 8, 8, 8, 8]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个指定值的3行5列\n",
    "np.full((3,5),8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "71a2bdc6-de17-4ac4-b27a-16c09690f3b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10.95884777, 22.35450589,  8.51530626, 32.28378365, 89.14422721],\n",
       "       [17.19168212, 90.65787872, 53.34975051,  2.77589633, 52.141061  ],\n",
       "       [18.99788783, 38.3615991 , 76.73250126, 61.18493816, 41.61795137]])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机创建一个3行5列的\n",
    "np.random.random((3,5))*100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1dc0b50b-2610-4329-afe6-de23b8a20d14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.25082446, 0.94675294, 0.18932038],\n",
       "       [0.17929141, 0.34988924, 0.23054125],\n",
       "       [0.67044574, 0.11507938, 0.89630937]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 基于某一随机因子\n",
    "from numpy.random import default_rng\n",
    "default_rng(12).random((3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5834098c-e898-4dbe-ad6e-a699d6d3a0c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.25082446, 0.94675294, 0.18932038],\n",
       "       [0.17929141, 0.34988924, 0.23054125],\n",
       "       [0.67044574, 0.11507938, 0.89630937]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#结果应该跟上面是一样的，因为因子是一样的\n",
    "default_rng(12).random((3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a8694a8a-a537-4676-864d-c13c692eaa60",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 0 0]\n",
      " [1 1 1 1 1]\n",
      " [2 2 2 2 2]\n",
      " [3 3 3 3 3]\n",
      " [4 4 4 4 4]]\n",
      "------------\n",
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]\n",
      " [0 1 2 3 4]\n",
      " [0 1 2 3 4]\n",
      " [0 1 2 3 4]]\n"
     ]
    }
   ],
   "source": [
    "# 通过indices生成坐标索引网格\n",
    "# 参数是一个大小，返回的会增加一维，第一维的第一个代表的行的索引，第一维第二个代表列的索引\n",
    "x, y = np.indices((5, 5))\n",
    "print(x)\n",
    "print(\"------------\")\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb3a53a5-301b-4320-8c89-4835fc302f06",
   "metadata": {},
   "source": [
    "### 截取，连接和更改一个现有的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "0c874b6f-6844-4bf7-9727-77bcade9e013",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3 2 3 4 5 6 7 8 9]\n",
      "[2 3]\n",
      "[4 5]\n"
     ]
    }
   ],
   "source": [
    "# 根据python的列表截取语法，也可以截取ndarray的数据，但是新数组是原数组的一个视图，所以修改新数组的时候也会修改原数组。\n",
    "# 如果想要获取新的数组，需要使用np.copy()方法\n",
    "base = np.arange(0,10)\n",
    "other = base[:2]\n",
    "other_copy = base[2:4].copy()\n",
    "other +=2\n",
    "other_copy +=2\n",
    "\n",
    "print(base)\n",
    "print(other)\n",
    "print(other_copy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "01bf0927-3bcc-4972-aacb-f561466f287c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将四个个2*2的二维数组，合并成一个4*4的二维数组\n",
    "base1 = np.ones((2,2))\n",
    "base2 = np.zeros((2,2))\n",
    "base3 = np.ones((2,2))\n",
    "base4 = np.zeros((2,2))\n",
    "np.block([[base1,base2],[base3,base4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "5457dadf-5c6a-44e0-a056-1f83b51bc5a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 0., 0., 1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0., 1., 1., 0., 0.]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "base1 = np.ones((2,2))\n",
    "base2 = np.zeros((2,2))\n",
    "base3 = np.ones((2,2))\n",
    "base4 = np.zeros((2,2))\n",
    "np.block([np.block([base1,base2]),np.block([base3,base4])])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "fed07f77-e0c6-48d7-93f4-f488ddef48a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0 0 0]\n",
      " [0 2 0 0 0]\n",
      " [0 0 3 0 0]\n",
      " [0 0 0 4 0]\n",
      " [0 0 0 0 5]]\n",
      "[[1 1 1 1 1]\n",
      " [1 1 1 1 1]\n",
      " [1 1 1 1 1]\n",
      " [1 1 1 1 1]\n",
      " [1 1 1 1 1]]\n",
      "[[0 0 0 0 0]\n",
      " [0 0 0 0 0]\n",
      " [0 0 0 0 0]\n",
      " [0 0 0 0 0]\n",
      " [0 0 0 0 0]]\n"
     ]
    }
   ],
   "source": [
    "# 创建与已存在的数组形状相同的数组\n",
    "base = np.diag([1,2,3,4,5])\n",
    "np.empty_like\n",
    "ones_like = np.ones_like(base)\n",
    "zeros_like = np.zeros_like(base)\n",
    "print(base)\n",
    "print(ones_like)\n",
    "print(zeros_like)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a65db6fb-b6d6-4860-ae14-5c57177f83f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['1', '2', '3', 'hello'], dtype='<U21')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果我的列表中不是一种类型的数据，会怎么样呢？\n",
    "li = [1,2,3,\"hello\"]\n",
    "np.array(li)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9a7802c-a3e7-4871-aad2-4b3bccfac029",
   "metadata": {},
   "source": [
    "上面的结果怎么感觉跟我们想想的不太一样呀，我们之前说过，ndarry中的元素应该是同一种类型，但是我们给的数据是有整型也有字符串的，\n",
    "虽然我们的列表中是存在两个数据类型，但是我们看打印结果中的dtype，代表的元素数据类型*<U21*。所以numpy会将不同的原始类型数据，进行。\n",
    "那这个*<U21*代表什么类型呢？其实在numpy中存在一些类型是可以自定义大小来保存数据的。这个*<U21*就是其中的一种，还有另外的几种\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fa0b350e-0c70-406c-9d5e-b271d7cf20bf",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'hello'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[7], line 3\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 如果我的列表中不是一种类型的数据，我指定数据类型呢？\u001b[39;00m\n\u001b[1;32m      2\u001b[0m li \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhello\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[0;32m----> 3\u001b[0m np\u001b[38;5;241m.\u001b[39marray(li,dtype\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mint64\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'hello'"
     ]
    }
   ],
   "source": [
    "# 如果我的列表中不是一种类型的数据，我指定数据类型呢？\n",
    "li = [1,2,3,\"hello\"]\n",
    "np.array(li,dtype=\"int64\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee797cb4-fd75-4988-8b40-2c9ed3c10f98",
   "metadata": {},
   "source": [
    "# 索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2e35eb5-5517-4042-a103-6fc70d7b49b2",
   "metadata": {},
   "source": [
    "与python一样，我们可以通过[]中括号加下标，来获取一个列表对应位置的数据，下标是以0开始的，负数是从数组最后一个元素开始算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8a8e207-828d-4867-afaa-9c7c1bc8b254",
   "metadata": {},
   "source": [
    "## 基础索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "8ad26e33-4732-4edc-a73d-df43ac4b0013",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一维的获取数据\n",
    "base = np.arange(0,10)\n",
    "base[5]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "3daa6f3b-ddac-4a05-b98c-bc125fca115f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维获取数据\n",
    "base.shape = (2,5)\n",
    "base[1,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "28cccdb9-992d-477f-ac84-f539f89affd5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果我的索引小于我的维数，那我会得到一个子数组\n",
    "base[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "1b432a14-567f-4784-8f4f-675686db17b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取这个子数组后，我们还可以在通过索引再获取\n",
    "base[0][1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64996c02-e977-4a1b-93e0-af582bd32a38",
   "metadata": {},
   "source": [
    "这样，我们获取一个二维数组中某一个元素会有两种方式，base[0][1]和base[0,1],\n",
    "在numpy中，我们更建议使用base[0,1]这种形式，由于我们获取的数据不是从原数组中拷贝出来的，而是一个视图，其内部数据指向的数据是一样的，如果我们使用base[0][1]这种方式，会先创建一个base[0]的临时一维数组，然后再获取下标是1的元素，相比base[0,1]这种方式的效率低。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a125424a-f3f5-4ef3-b809-f96441cf012f",
   "metadata": {},
   "source": [
    "## 切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e9e5f13-d24e-4abe-9928-a45a42df14bf",
   "metadata": {},
   "source": [
    "切片与python中也是相似的，不同的是python中的切片是副本，而numpy中的切片是视图。\n",
    "基本语法是 [start:end:step],start代表开始位置，end代表结束位置，step代表步长。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "fb67f11a-fd58-4605-851d-70a11562b4c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 2]\n",
      "[1, 2, 3, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "# python中列表的切片是副本,所以切片出来的数据变化了，原数据不会跟着变化\n",
    "a = [1,2,3,5,6]\n",
    "b = a[:2]\n",
    "b[0] = 12\n",
    "print(b)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "a21ff531-d64b-4a81-86eb-92b084e4be71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12  1]\n",
      "[12  1  2  3  4  5  6  7  8  9]\n"
     ]
    }
   ],
   "source": [
    "# numpy中的切片是视图，如果切片出来的数据发生变化了，原数据也会跟着变化\n",
    "an = np.arange(0,10)\n",
    "bn = an[:2]\n",
    "bn[0] = 12\n",
    "print(bn)\n",
    "print(an)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "197d6f57-c18b-4634-87b8-e8d7b487970f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 2],\n",
       "        [1, 2]]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多维切片，多个：切片，中间用逗号隔开，\n",
    "a2 = np.array([[[1,2,3,4],[1,2,3,4]],[[5,6,7,8],[5,6,7,8]]])\n",
    "a2[0:1,0:2,0:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae048cb1-f0a3-4f6a-890b-029abb2bf2c9",
   "metadata": {},
   "source": [
    "### 三个点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d9cce9e2-d0c6-4599-93e0-5f70e5c11b89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 1 2 3]\n",
      " [4 5 6 4 5 6]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(2, 6)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1,2,3,1,2,3], [4,5,6,4,5,6]])\n",
    "print(x)\n",
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "b143ec6a-cb71-4b5d-bcb2-3f4666522564",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 6])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[...,2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ff0a292-e617-4922-8403-d1e0fc4a0606",
   "metadata": {},
   "source": [
    "三个点，就是获取所有的，类似与一个维度的冒号(：)前后不带数字，但是三个点可以代表多个维度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "449fe1f0-956d-4077-8899-d4e1689b091f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:pytorchenv]",
   "language": "python",
   "name": "conda-env-pytorchenv-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
